package com.example.config;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.entity.ShopList;
import com.example.mapper.ShopListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: tobed
 * @date: 2022/11/21
 * @time: 14:00
 */
@EnableScheduling
@Configuration
public class ShopTimeJob {

    @Autowired
    ShopListMapper shopListMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /*定时任务24H一次*/
    //    @Scheduled(cron = "0 0 1 1-31 * ? ")
//    @Scheduled(fixedDelay = 1000)
    public void listShopTime(){
        String ifSelect = stringRedisTemplate.opsForValue().get("ifSelect");
        if (ifSelect==null){
            stringRedisTemplate.delete("ShopList");
            Example example = new Example(ShopList.class);
            List<ShopList> shopLists = shopListMapper.selectByExample(example);
            ArrayList<ShopList> list = new ArrayList<>();
            shopLists.stream().forEach(shopList -> {
                /*将查询时间转化为LocalDateTime*/
                Instant instant = shopList.getStartTime().toInstant();
                ZoneId zoneId = ZoneId.systemDefault();
                LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
                /*再获取当日LocalDateTime*/
                LocalDateTime today = LocalDateTime.now();
                if (localDateTime.getDayOfMonth() == today.getDayOfMonth() && localDateTime.getMonth() == today.getMonth()){
                    long times = shopList.getStartTime().getTime() - new Date().getTime();
                    shopList.setShopOpenTime(times);
                    list.add(shopList);
                    System.out.println("-------"+shopList.getShopName()+"时间-----"+shopList.getStartTime());
                }
                if (list.size()>0){
                    stringRedisTemplate.opsForValue().set("ShopList",JSONObject.toJSONString(list));
                    stringRedisTemplate.opsForValue().set("ifSelect",111+"",24, TimeUnit.HOURS);
                }
            });
        }
    }

//    @Scheduled(fixedDelay = 100)
    public void updatesss(){
        List<ShopList> shopLists = JSONUtil.toBean(stringRedisTemplate.opsForValue().get("ShopList"), new TypeReference<List<ShopList>>() {
        }, true);
        if (shopLists != null){
            for (int i = 0; i < shopLists.size(); i++) {
                Date date = new Date();
                if (date.getTime() == shopLists.get(i).getStartTime().getTime() || date.getTime() > shopLists.get(i).getStartTime().getTime() || shopLists.get(i).getStartTime().getTime() - date.getTime() < 90){
                    if(shopLists.get(i).getStatus() == 0){
                        shopLists.get(i).setStatus(1);
                        stringRedisTemplate.opsForValue().set("ShopList",JSONObject.toJSONString(shopLists));
                        int i1 = shopListMapper.updateStatus(shopLists.get(i));
                    }
                }
            }
        }
    }

//    /*判断是否满足条件 每10分钟一次*/
//    @Scheduled(fixedDelay = 6000000)
//    public void updateStatus(){
//        List<ShopList> shopLists = JSONUtil.toBean(stringRedisTemplate.opsForValue().get("ShopList"), new TypeReference<List<ShopList>>() {
//        }, true);
//        shopLists.stream().forEach(new Consumer<ShopList>() {
//            @Override
//            public void accept(ShopList shopList) {
//                /*将查询时间转化为LocalDateTime*/
//                Instant instant = shopList.getStartTime().toInstant();
//                ZoneId zoneId = ZoneId.systemDefault();
//                LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
//                /*再获取当日LocalDateTime*/
//                LocalDateTime today = LocalDateTime.now();
//                ArrayList<ShopList> list = new ArrayList<>();
//                if (localDateTime.getSecond() == today.getSecond()){
//                    list.add(shopList);
//                    getMinute(list);
//                }
//            }
//        });
//    }
//
//    /*每一分钟一次*/
//    @Scheduled(fixedDelay = 600000)
//    public static void getMinute(List<ShopList> list){
//        list.stream().forEach(new Consumer<ShopList>() {
//            @Override
//            public void accept(ShopList shopList) {
//                /*将查询时间转化为LocalDateTime*/
//                Instant instant = shopList.getStartTime().toInstant();
//                ZoneId zoneId = ZoneId.systemDefault();
//                LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
//                /*再获取当日LocalDateTime*/
//                LocalDateTime today = LocalDateTime.now();
//                ArrayList<ShopList> list = new ArrayList<>();
//                if (localDateTime.getMinute() == today.getMinute()){
//                    list.add(shopList);
//                    getSecond(list);
//                }
//            }
//        });
//    }
//
//    /*每一秒钟一次*/
//    @Scheduled(fixedDelay = 1000)
//    public static void getSecond(List<ShopList> list){
//        for (int i = 0; i < list.size(); i++) {
//            /*将查询时间转化为LocalDateTime*/
//            ShopList shopList = list.get(i);
//            Instant instant = shopList.getStartTime().toInstant();
//            ZoneId zoneId = ZoneId.systemDefault();
//            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
//            /*再获取当日LocalDateTime*/
//            LocalDateTime today = LocalDateTime.now();
//            if (localDateTime.getSecond() == today.getSecond()){
////                int i1 = shopListMapper.updateStatus(shopList);
//            }
//        }
////        list.stream().forEach(new Consumer<ShopList>() {
////            @Override
////            public void accept(ShopList shopList) {
////                /*将查询时间转化为LocalDateTime*/
////                Instant instant = shopList.getStartTime().toInstant();
////                ZoneId zoneId = ZoneId.systemDefault();
////                LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
////                /*再获取当日LocalDateTime*/
////                LocalDateTime today = LocalDateTime.now();
////                if (localDateTime.getSecond() == today.getSecond()){
////                    int i = ShopListMapper.updateStatus(shopList);
////                    if (i!=0){list.remove(shopList);}
////                }
////            }
////        });
//    }

}

